﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace kindly
{
    public partial class GetFile :LoggingPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fileName = Request.QueryString["file"];
            if (string.IsNullOrEmpty(fileName))
            {
                fileNotFound();
                return;
            }

            string filePath = Files.GetFilePath(fileName);

            FileInfo file = new FileInfo(filePath);
            if (file != null && file.Exists)
            {
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.WriteFile(file.FullName);
                //Response.Close();
            }
            else
            {
                fileNotFound();
            }
        }

        private void fileNotFound()
        {
            Response.Clear();
            Response.StatusCode = 404;
            Response.StatusDescription = "File not found";
            Response.Flush();
            Response.Close();
        }
    }
}